using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web;

namespace ModWatch
{

	/// <summary>
	/// An implementaion of <see cref="System.Web.UI.WebControls.CheckBoxList"/>
	/// that notifies the user if a change has been made to any
	/// <see cref="ModWatch.IModWatcher"/> objects.
	/// </summary>
	[Obsolete("The recommended alternative is ModWatch.OnCheckBoxListChangedModAlertExtender", true)]
	public class ModAlertCheckBoxList : CheckBoxList, IScriptControl
	{

		/// <summary>
		/// Gets or sets the modification group to be checked for changes.
		/// </summary>
		public string ModGroup
		{
			get { return (string)ViewState["ModGroup"]; }
			set { ViewState["ModGroup"] = value; }
		}

		/// <summary>
		/// Gets or sets the dialog message that should prompt the user if a change exists.
		/// </summary>
		public string NotifyModMessage
		{
			get { return (string)ViewState["NotifyModMessage"]; }
			set { ViewState["NotifyModMessage"] = value; }
		}

		/// <summary>
		/// Renders a list item in the <see cref="ModWatch.ModAlertCheckBoxList"/> control.
		/// </summary>
		/// <param name="itemType">One of the <see cref="ListItemType"/> enumeration values.</param>
		/// <param name="repeatIndex">An ordinal index that specifies the location of the item in the list control.</param>
		/// <param name="repeatInfo">
		/// A <see cref="RepeatInfo"/> object that represents the information used to render the item in the list.
		/// </param>
		/// <param name="writer">
		/// An <see cref="HtmlTextWriter"/> object that represents the output stream to render HTML content on the client.
		/// </param>
		protected override void RenderItem(ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
		{
			ListItem item = this.Items[repeatIndex];

			string onclick = null;
			if (item.Attributes != null)
			{
				onclick = item.Attributes["onclick"];
				if (onclick != null && onclick.Trim().Length > 0)
				{
					base.Attributes.Remove("onclick");
				}
			}

			StringBuilder newOnClick = new StringBuilder();
			newOnClick.Append("if ($find('");
			newOnClick.AppendFormat("{0}_{1}", this.ClientID, repeatIndex);
			newOnClick.Append("').checkForChange())");
			newOnClick.Append(" return false;");
			newOnClick.Append(onclick);

			item.Attributes.Add("onclick", newOnClick.ToString());

			base.RenderItem(itemType, repeatIndex, repeatInfo, writer);
		}

		/// <summary>
		/// Raises the <see cref="System.Web.UI.Control.PreRender"/> event.
		/// </summary>
		/// <param name="e">
		/// An <see cref="System.EventArgs"/> object that contains the event data.
		/// </param>
		protected override void OnPreRender(EventArgs e)
		{
			if (!this.DesignMode)
			{
				// Test for ScriptManager and register if it exists
				ScriptManager sm = ScriptManager.GetCurrent(Page);

				if (sm == null)
					throw new HttpException("A ScriptManager control must exist on the current page.");

				sm.RegisterScriptControl(this);
			}

			base.OnPreRender(e);
		}

		/// <summary>
		/// Displays the <see cref="ModWatch.ModAlertCheckBoxList"/>
		/// on the client.
		/// </summary>
		/// <param name="writer"></param>
		protected override void Render(HtmlTextWriter writer)
		{
			if (!this.DesignMode)
			{
				ScriptManager sm = ScriptManager.GetCurrent(Page);
				sm.RegisterScriptDescriptors(this);
			}

			base.Render(writer);
		}

		#region IScriptControl Members

		/// <summary>
		/// Returns the <see cref="System.Web.UI.ScriptDescriptor"/>s required by
		/// this instance of <see cref="ModWatch.ModAlertCheckBoxList"/>.
		/// </summary>
		/// <returns>
		/// A collection of <see cref="System.Web.UI.ScriptDescriptor"/>s
		/// defining this instance of <see cref="ModWatch.ModAlertCheckBoxList"/>.
		/// </returns>
		public IEnumerable<ScriptDescriptor> GetScriptDescriptors()
		{
			for (int i = 0; i < this.Items.Count; i++)
			{
				ScriptControlDescriptor scd = new ScriptControlDescriptor("ModWatch.ModAlertCheckBox", String.Format("{0}_{1}", this.ClientID, i));

				scd.AddProperty("modGroup", this.ModGroup);
				scd.AddProperty("notifyModMessage", this.NotifyModMessage);

				yield return scd;
			}
		}

		/// <summary>
		/// Returns the <see cref="ModWatch.ModWatchExtenderBase.GettingScriptReferences"/>
		/// objects needed by the <see cref="ModWatch.ModAlertCheckBoxList"/>.
		/// </summary>
		/// <returns>
		/// The <see cref="ModWatch.ModWatchExtenderBase.GettingScriptReferences"/>
		/// objects needed by the <see cref="ModWatch.ModAlertCheckBoxList"/>.
		/// </returns>
		public IEnumerable<ScriptReference> GetScriptReferences()
		{
			ScriptReference sr = new ScriptReference();
			sr.Path = Page.ClientScript.GetWebResourceUrl(typeof(ModAlertCheckBoxList), "ModWatch.Scripts.ModAlertCheckBox.js");

			return new ScriptReference[] { sr };
		}

		#endregion

	}

}
